{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mindquantum import *\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.bpansatz import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"584.64\" height=\"356.0\">\n",
       "<rect x=\"20\" y=\"10\" width=\"556.8\" height=\"320\" fill=\"#ffffff\" />\n",
       "<text x=\"40.0\" y=\"50.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"40.0\" y=\"110.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"40.0\" y=\"170.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<text x=\"40.0\" y=\"230.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q3:\n",
       " </text>\n",
       "<text x=\"40.0\" y=\"290.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q4:\n",
       " </text>\n",
       "<line x1=\"68.8\" x2=\"556.8\" y1=\"50.0\" y2=\"50.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"68.8\" x2=\"556.8\" y1=\"110.0\" y2=\"110.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"68.8\" x2=\"556.8\" y1=\"170.0\" y2=\"170.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"68.8\" x2=\"556.8\" y1=\"230.0\" y2=\"230.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"68.8\" x2=\"556.8\" y1=\"290.0\" y2=\"290.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"92.8\" y=\"30.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"112.8\" y=\"46.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"112.8\" y=\"62.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "π/4\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"92.8\" y=\"90.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"112.8\" y=\"106.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"112.8\" y=\"122.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "π/4\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"92.8\" y=\"150.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"112.8\" y=\"166.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"112.8\" y=\"182.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "π/4\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"92.8\" y=\"210.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"112.8\" y=\"226.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"112.8\" y=\"242.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "π/4\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"92.8\" y=\"270.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"112.8\" y=\"286.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"112.8\" y=\"302.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "π/4\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"152.8\" y=\"30.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"192.8\" y=\"46.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RZ\n",
       " </text>\n",
       "<text x=\"192.8\" y=\"62.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "θ(0,0)\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"152.8\" y=\"90.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"192.8\" y=\"106.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"192.8\" y=\"122.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "θ(0,1)\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"152.8\" y=\"150.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"192.8\" y=\"166.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"192.8\" y=\"182.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "θ(0,2)\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"152.8\" y=\"210.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"192.8\" y=\"226.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"192.8\" y=\"242.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "θ(0,3)\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"152.8\" y=\"270.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"192.8\" y=\"286.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"192.8\" y=\"302.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "θ(0,4)\n",
       " </text>\n",
       "\n",
       "<circle cx=\"272.8\" cy=\"110.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"50.0\" y2=\"110.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"30.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"50.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<circle cx=\"332.8\" cy=\"170.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"332.8\" x2=\"332.8\" y1=\"110.0\" y2=\"170.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"312.8\" y=\"90.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"110.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<circle cx=\"392.8\" cy=\"230.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"392.8\" x2=\"392.8\" y1=\"170.0\" y2=\"230.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"372.8\" y=\"150.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"392.8\" y=\"170.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<circle cx=\"452.8\" cy=\"290.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"452.8\" x2=\"452.8\" y1=\"230.0\" y2=\"290.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"432.8\" y=\"210.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"452.8\" y=\"230.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "<circle cx=\"512.8\" cy=\"290.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"512.8\" x2=\"512.8\" y1=\"50.0\" y2=\"290.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"492.8\" y=\"30.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"512.8\" y=\"50.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "</svg>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x19bf78136c8>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "产生Google那篇barren plateaus文章的随机线路\n",
    "bpansatz(n,p)\n",
    "n:比特数\n",
    "p:层数\n",
    "'''\n",
    "bpansatz(5,1).svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.train_nisq import noise_generator, get_rs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "经过标准差为0.01的高斯噪音修饰过的参数: [1.01466946 1.01873457 0.99635453]\n"
     ]
    }
   ],
   "source": [
    "params = np.array([1,1,1])\n",
    "sigma = 0.01\n",
    "'''\n",
    "noise_generator(params,sigma)\n",
    "params:要施加噪音的参数array，sigma:高斯噪音的标准差\n",
    "'''\n",
    "noisy_params = noise_generator(params, sigma)\n",
    "print(f'经过标准差为{sigma}的高斯噪音修饰过的参数:',noisy_params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.5 0.  0.  0.5]\n",
      " [0.  0.  0.  0. ]\n",
      " [0.  0.  0.  0. ]\n",
      " [0.5 0.  0.  0.5]]\n",
      "\n",
      " 第一个比特的约化密度矩阵\n",
      " [[0.5 0. ]\n",
      " [0.  0.5]]\n",
      "\n",
      " 第二个比特的约化密度矩阵\n",
      " [[0.5 0. ]\n",
      " [0.  0.5]]\n"
     ]
    }
   ],
   "source": [
    "rho_bellstate = np.array([[1,0,0,1],\n",
    "               [0,0,0,0],\n",
    "               [0,0,0,0],\n",
    "               [1,0,0,1]])*0.5\n",
    "print(rho_bellstate)\n",
    "'''\n",
    "get_rs(rho, list_qubits2keep)\n",
    "传入参数，某个密度矩阵：rho，list包含要保留的比特位的编号：list_qubits2keep\n",
    "'''\n",
    "rho0 = get_rs(rho_bellstate, [0])\n",
    "rho1 = get_rs(rho_bellstate, [1])\n",
    "print('\\n 第一个比特的约化密度矩阵\\n',rho0)\n",
    "print('\\n 第二个比特的约化密度矩阵\\n',rho1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:mindspore_py37] *",
   "language": "python",
   "name": "conda-env-mindspore_py37-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
